package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Controller.CompanyManagerController;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * this is the GUI for choosing dates for performance repots. 
 * @author Shahar Shelly
 *
 */
public class ChooseDatePerformance extends JFrame {
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	public JPanel getLastPanel()
	{
		return (JPanel)lastPanel;
	}
	private JPanel contentPane;

	/**
	 * is the constructor. it has listens for buttons of watch report and cancel, after choosing the dates by the combo box.
	 */
	public ChooseDatePerformance() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 340, 172);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final Date start=new Date(); start.setDate(1);
		final Date end=new Date(); start.setDate(1);
		
		JLabel lblPeriodicReport = new JLabel("Performance Report");
		lblPeriodicReport.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblPeriodicReport.setBounds(90, 11, 199, 26);
		contentPane.add(lblPeriodicReport);
		
		final JComboBox<Integer> Month = new JComboBox<Integer>();
		Month.setBounds(110, 60, 45, 20);
		contentPane.add(Month);
		for(int i=1; i<=12; i++) Month.addItem(i);
		
		final JComboBox<Integer> Year = new JComboBox<Integer>();
		Year.setBounds(165, 60, 52, 20);
		contentPane.add(Year);
		for(int i=2000; i<=3000; i++) Year.addItem(i);
		
		JButton btnWatch = new JButton("Watch");
		btnWatch.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) 
			{
				GregorianCalendar calendar = new GregorianCalendar();
				end.setMonth((int)Month.getSelectedItem());
				end.setMonth((int)Year.getSelectedItem());
				calendar.setTime(end);
				calendar.roll(Calendar.MONTH, false);
				start.setMonth(calendar.getTime().getMonth());
				if(end.getMonth()==1)
					start.setYear(end.getYear()-1);
				else
					start.setYear(end.getYear());
				CompanyManagerController.viewPerformanceReport(start, end);
			}
		});
		btnWatch.setBounds(66, 100, 89, 23);
		contentPane.add(btnWatch);
		
		JButton btnReturn = new JButton("Return");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
				frame.setContentPane(lastPanel);
				frame.setVisible(true);
			}
		});
		btnReturn.setBounds(175, 100, 89, 23);
		contentPane.add(btnReturn);
	}
	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
}
